//
//  QHPickerView.m
//  QHealthForDoctor
//
//  Created by careate-hubing on 2020/2/26.
//  Copyright © 2020 李 志良. All rights reserved.
//

#import "QHPickerView.h"
#import "QHPickerToolBar.h"

static CGFloat const PickerViewHeght = 240;

static CGFloat const PickerViewLabelWeight = 32;

@interface QHPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) UIPickerView *pickerView;

@property (nonatomic,strong) UIView *line;

@property (nonatomic,strong) QHPickerToolBar *toolBar;



@property (nonatomic,strong) NSString *selectStr;

@property (nonatomic,assign) NSInteger selectIndex;


@end

@implementation QHPickerView

+ (instancetype)selectData:(SelectDataBlock)selectDataBlock{
    QHPickerView *newPickerView = [[QHPickerView alloc]init];
    newPickerView.selectDataBlock = selectDataBlock;
    return newPickerView;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        [self customUI];
        [self loadData];
    }
    return self;
}

-(void)customUI{
    self.bounds = [UIScreen mainScreen].bounds;
    self.backgroundColor = RGBA(0, 0, 0, 102.0/255);
    [self.layer setOpacity:0.0];
    [self addSubview:self.pickerView];
    [self.pickerView addSubview:self.line];
    [self addSubview:self.toolBar];
    [self addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
}

-(UIView *)line{
    if (!_line) {
        _line =  [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        [_line setBackgroundColor:RGBCOLOR(205, 205, 205)];
    }
    return _line;
}

-(UIPickerView *)pickerView{
    if (!_pickerView) {
        CGFloat pickerW = SCREEN_WIDTH;
        CGFloat pickerH = PickerViewHeght - 44;
        CGFloat pickerX = 0;
        CGFloat pickerY = SCREEN_HEIGHT +44;
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(pickerX, pickerY, pickerW, pickerH)];
        [_pickerView setBackgroundColor:[UIColor whiteColor]];
        [_pickerView setDataSource:self];
        [_pickerView setDelegate:self];
    }
    return _pickerView;
}

-(QHPickerToolBar *)toolBar{
    if (!_toolBar) {
        _toolBar = [[QHPickerToolBar alloc]initWithTitle:@"选择时间" cancelButtonTitle:@"取消" okButtonTitle:@"确定" addTarget:self cancelAction:@selector(dismiss) okAction:@selector(selectedOk)];
        CGRect frame = _toolBar.frame;
        frame.origin.x = 0;
        frame.origin.y = SCREEN_HEIGHT;
        _toolBar.frame = frame;
    }
    return _toolBar;
}

-(void)selectedOk{
    if (self.selectDataBlock) {
        self.selectStr = self.dataSource[self.selectIndex];
        self.selectDataBlock( self.selectStr, self.selectIndex);
    }
    [self dismiss];
}

-(void)loadData
{
    NSMutableArray *temp = [NSMutableArray array];
    for (int i = 1; i < 32; i ++) {
        NSString *dateStr = [NSString stringWithFormat:@"%d日",i];
        [temp addObject:dateStr];
    }
    _dataSource = [temp copy];
    NSString *title = _dataSource[self.selectIndex];
    [self.toolBar setTitle:title];
}

-(void)setDataSource:(NSArray *)dataSource
{
    _dataSource = dataSource;
    if (self.selectIndex > dataSource.count) {
        self.selectIndex = dataSource.count/2;
    }
    [self.toolBar setTitle:_dataSource[self.selectIndex]];
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:self.selectIndex inComponent:0 animated:YES];
}
#pragma mark - --- delegate 视图委托 ---
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _dataSource.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return PickerViewLabelWeight;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UILabel* label = [[UILabel alloc] initWithFrame: CGRectMake(8, 0, self.frame.size.width/4 - 16, 30)];
    label.adjustsFontSizeToFitWidth = NO;
    label.font = [UIFont systemFontOfSize: 14];
    label.textAlignment = NSTextAlignmentCenter;
    NSString *text = _dataSource[row];
    if (text.length != 0) { label.text = text; }
    return label;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *str = _dataSource[row];
    [self.toolBar setTitle:str];
    self.selectStr = str;
    self.selectIndex = row;
}


-(void)showWithIndex:(NSInteger)index
{
    __weak typeof(self) ws = self;
    __block NSInteger temp = index;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    CGRect frameTool = self.toolBar.frame;
    frameTool.origin.y -= PickerViewHeght;
    CGRect framePicker =  self.pickerView.frame;
    framePicker.origin.y -= PickerViewHeght;
    [UIView animateWithDuration:0.33 animations:^{
        [ws.layer setOpacity:1];
        ws.toolBar.frame = frameTool;
        ws.pickerView.frame = framePicker;
        if (temp > ws.dataSource.count) {
            temp = ws.dataSource.count/2;
        }
        ws.selectIndex = temp;
        [ws.pickerView selectRow:temp inComponent:0 animated:YES];
        NSString *title = ws.dataSource[temp];
        [ws.toolBar setTitle:title];
    } completion:^(BOOL finished) {
    }];
}

-(void)dismiss{
    __weak typeof(self) ws = self;
    CGRect frameTool = self.toolBar.frame;
    frameTool.origin.y += PickerViewHeght;
    
    CGRect framePicker = self.pickerView.frame;
    framePicker.origin.y += PickerViewHeght;
    [UIView animateWithDuration:0.33 animations:^{
        [ws.layer setOpacity:0];
        ws.toolBar.frame = frameTool;
        ws.pickerView.frame = framePicker;
    } completion:^(BOOL finished) {
        [ws removeFromSuperview];
    }];
}


@end
